/**
 * Created by wangjian on 17/5/3.
 */
myApp.controller('ChangePlanProjectCtrl', function ($scope, $uibModal, $uibModalInstance, info, $http, ngDialog) {
    $scope.info = info;
    $scope.info.planInfo = {};
    $scope.info.projectType = '0';
    //选择已有项目
    $scope.info.proList = [];
    $scope.info.selectProName = '';
    //选择新建项目
    $scope.info.svn_form = '';
    $scope.info.proInfo = {};
    $scope.info.msg = '';

    $scope.getPlanInfo = function () {
        doBeetleGet($http,$uibModal,beetle_plan_url + '/project_plan/getPlanList?hasProList=true&isShowOnline=true&token_id=' + getTokenId() + '&plan_id=' + $scope.info.planId,function (data) {
            $scope.info.planInfo = data.data[0];
        })
    };

    $scope.getPlanInfo();

    //获取主干下工程列表
    $scope.getProListByTrunk = function () {
        doBeetlePost($http,$uibModal,java_url + '/beetle/project/projectListByTrunk', {
            tokenId: getTokenId(),
            trunkName: $scope.info.trunkName
        },function (data) {
            if (data.code == '0') {
                $scope.info.proList = data.data.list;
                if ($scope.info.proList.length > 0) {
                    $scope.info.selectProName = $scope.info.proList[0].projectName;
                }
            }
        });
    };

    $scope.updateBranch_num = function () {
        var branch_num = $scope.info.proInfo.branch_num;
        var t = /^(\d{1,4}){1}(\.\d{1,4}){2}$/;
        if (branch_num.match(t)) {
            $scope.info.msg = '';
        } else {
            $scope.info.msg = '请输入正确的版本号！格式：0.0.0';
        }
    };

    $scope.updatePlanCode = function () {
        if ($scope.info.planInfo.plan_code != '') {
            $scope.info.msg = '';
        } else {
            $scope.info.msg = '项目代号不能为空!';
        }
    };

    $scope.proTypeChange = function () {
        if ($scope.info.projectType == '0') {
            var list = [$scope.info.trunkName];
            LoadModal($uibModal);
            $http.get(beetle_plan_url + '/project_plan/getPlanProjectDefaultName?token_id=' + getTokenId() + '&plan_code=' + $scope.info.planInfo.plan_code + '&trunk_list=' + JSON.stringify(list)).success(function (data) {
                $scope.info.proInfo = data.data[0];
                $scope.info.svn_form = $scope.info.proInfo.svn_list[0].name;
                closeModal();
            })
        } else if ($scope.info.projectType == '1') {
            $scope.getProListByTrunk();
        }
    };

    $scope.proTypeChange();

    //切换
    $scope.changeToBranch = function () {
        openDialog(ngDialog, $scope, "确定要切换到新工程吗？", true, function (val) {
            if (!val) {
                return;
            }
            openDialog(ngDialog, $scope, "切换后，将使用新工程进行开发与提测，请再次确认？", true, function (val) {
                if (!val) {
                    return;
                }
                if ($scope.info.projectType == '0') {
                    var projectName = $scope.info.proInfo.trunk_name + '-' + $scope.info.proInfo.branch_num + '.' + $scope.info.planInfo.plan_code;
                    LoadModal($uibModal);
                    $http.get(beetle_plan_url + '/project_plan/changePlanProjectToNewBranch?token_id=' + getTokenId() + '&plan_id=' + $scope.info.planId + '&plan_code=' + $scope.info.planInfo.plan_code + '&plan_name=' + $scope.info.planInfo.plan_name + '&trunk_name=' + $scope.info.trunkName + '&project_name=' + projectName + '&branchNum=' + $scope.info.proInfo.branch_num + '&fromProject=' + $scope.info.svn_form).success(function (data) {
                        if (data.success == true) {
                            $uibModalInstance.dismiss('save');
                        } else {
                            alert(data.msg)
                        }
                    }).error(function (data) {
                        alert(data.message);
                    }).finally(function () {
                        closeModal();
                    })
                } else if ($scope.info.projectType == '1') {
                    LoadModal($uibModal);
                    $http.get(beetle_plan_url + '/project_plan/changePlanProjectBranch?token_id=' + getTokenId() + '&trunk_name=' + $scope.info.trunkName + '&plan_id=' + $scope.info.planId + '&project_name=' + $scope.info.selectProName).success(function (data) {
                        if (!data.success) {
                            alert(data.msg)
                        }
                    }).error(function (data) {
                        alert(data.message);
                    }).finally(function () {
                        closeModal();
                        $uibModalInstance.dismiss('save');
                    })
                }
            })
        });
    };

    $scope.save = function () {
        $scope.changeToBranch();
    };

    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };

    $scope.checkProjectPlanCode = function () {
        if ($scope.info.planInfo.plan_code == '') {
            alert("请输入项目代号!");
            return;
        }
        var list = [$scope.info.trunkName];
        LoadModal($uibModal);
        $scope.info.msg = '';
        $http.get(beetle_plan_url + '/project_plan/checkProjectPlanCode?token_id=' + getTokenId() + '&plan_code=' + $scope.info.planInfo.plan_code + '&project_names=' + JSON.stringify(list)).success(function (data) {
            if (!data.success) {
                $scope.info.msg = data.msg;
            }
        }).error(function (data) {
            alert(data.message);
        }).finally(function () {
            closeModal();
        })

    };
});